﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.Web.Caching;
using System.IO;
using System.Reflection;
using System.Collections;

namespace Core.Plugin {
    public class AssemblyResourceProvider : System.Web.Hosting.VirtualPathProvider {

        private bool IsAppResourcePath(string virtualPath) {
            String checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
            bool IsShared = checkPath.ToLower().Contains("editortemplates");
            if(IsShared)
                IsShared = checkPath.ToLower().Contains("shared");
            bool fnd = checkPath.StartsWith("~/Plugins/", StringComparison.InvariantCultureIgnoreCase);
            if(fnd)
                fnd = fnd && checkPath.ToLower().Contains(".dll");
            if(checkPath.ToLower().Contains("Wiki"))
                fnd = true;
            return fnd;
        }

        public override bool FileExists(string virtualPath) {
            if (IsAppResourcePath(virtualPath)) {
                string path = VirtualPathUtility.ToAppRelative(virtualPath);
                string[] parts = path.Split('/');
                string assemblyName = parts[2];
                string resourceName = parts[3];
                if (parts.Count() > 4) 
                    resourceName += "." + parts[4];

                assemblyName = Path.Combine(HttpRuntime.BinDirectory, assemblyName);
                byte[] assemblyBytes = File.ReadAllBytes(assemblyName);
                Assembly assembly = Assembly.Load(assemblyBytes);

                if (assembly != null) {
                    string[] resourceList = assembly.GetManifestResourceNames();
                    bool found = Array.Exists(resourceList, delegate(string r) { return r.Equals(resourceName); });

                    return found;
                }
                return false;
            }
            else
                return base.FileExists(virtualPath);
        }

        public override VirtualFile GetFile(string virtualPath) {
            if (IsAppResourcePath(virtualPath))
                return new AssemblyResourceVirtualFile(virtualPath);
            else
                return base.GetFile(virtualPath);
        }

        public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) {
            if (IsAppResourcePath(virtualPath)) {
                //string[] parts = virtualPath.Split('/');
                //string assemblyName = parts[2];

                //assemblyName = Path.Combine(_PluginDirectory, assemblyName);

                //return new CacheDependency(assemblyName);
                return null;
            }
            return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
        }
    }
}

